package org.yenn.pattern.aliyun.behavioral.observer;

import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

/**
 * 步骤 1
 * 创建 Subject 类。
 */
public class Subject {
    private List<Observer> observers = new ArrayList();

    @Getter
    private int state;


    public void setState(int state) {
        this.state = state;
        notifyAllObservers();
    }

    public void attach(Observer observer) {
        observers.add(observer);
    }

    public void notifyAllObservers() {
        for (Observer observer : observers) {
            observer.update();
        }
    }
}